﻿using COSXML.Model.Object;
using COSXML.Auth;
using COSXML.Transfer;
using System;
using COSXML;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using COSXML.Model.Bucket;
using COSXML.CosException;


#region
namespace COSSnippet
{
    public class Download
    {
        public async void DDownload(string filename,string filepath,string document)
        {
            //初始化 CosXmlConfig 
            //string appid = "1250000000";//设置腾讯云账户的账户标识 APPID
            string region = "ap-shanghai"; //设置一个默认的存储桶地域
            CosXmlConfig config = new CosXmlConfig.Builder()
              .IsHttps(true)  //设置默认 HTTPS 请求
              .SetRegion(region)  //设置一个默认的存储桶地域
              .SetDebugLog(true)  //显示日志
              .Build();  //创建 CosXmlConfig 对象
            string secretId = ("AKIDNf2PoDQfjgaHtJZI2mLhoi1eEeuAF9sP"); //用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            string secretKey = ("Yv8A1iFbnphNJ4eQOEzkGex6CC5qOhkW"); //用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            long durationSecond = 600;  //每次请求签名有效时长，单位为秒
            QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
            CosXml cosXml = new CosXmlServer(config, cosCredentialProvider);
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();

            // 初始化 TransferManager
            TransferManager transferManager = new TransferManager(cosXml, transferConfig);

            String bucket = "document-manage-1317383760"; //存储桶，格式：BucketName-APPID
            String cosPath ="/"+document; //对象在存储桶中的位置标识符，即称对象键
            string localDir = filepath;//本地文件夹
            string localFileName = filename; //指定本地保存的文件名

            // 下载对象
            COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
              localDir, localFileName);

            downloadTask.progressCallback = delegate (long completed, long total)
            {
                Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
            };

            try
            {
                COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await transferManager.DownloadAsync(downloadTask);
                Console.WriteLine(result.GetResultInfo());
                string eTag = result.eTag;
            }
            catch (Exception e)
            {
                Console.WriteLine("CosException: " + e);
            }
        }
    }
    public class TransferDownloadObjectModel
    {

        private CosXml cosXml;

        public TransferDownloadObjectModel()
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
              .SetRegion("ap-shanghai") // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
              .Build();

            string secretId = "AKIDNf2PoDQfjgaHtJZI2mLhoi1eEeuAF9sP";   // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            string secretKey = "Yv8A1iFbnphNJ4eQOEzkGex6CC5qOhkW"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            long durationSecond = 10;          //每次请求签名有效时长，单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
              secretKey, durationSecond);

            this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }

        /// 高级接口下载对象
        public async Task TransferDownloadObject(string filename,string filepath)
        {
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();
            // 手动设置高级下载接口的分块阈值为 20MB(默认为20MB), 从5.4.26版本开始支持！
            //transferConfig.DivisionForDownload = 20 * 1024 * 1024;
            // 手动设置高级下载接口的分块大小为 10MB(默认为5MB),不建议此处设置过小的分块值,可能导致频繁重试或下载速度不合预期
            //transferConfig.SliceSizeForDownload = 10 * 1024 * 1024;

            // 初始化 TransferManager
            TransferManager transferManager = new TransferManager(cosXml, transferConfig);
            String bucket = "document-manage-1317383760"; //存储桶，格式：BucketName-APPID
            String cosPath = "/实习总结报告.docx"; //对象在存储桶中的位置标识符，即称对象键
            string localDir = filepath;//本地文件夹
            string localFileName = filename; //指定本地保存的文件名
            // 下载对象
            COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
              localDir, localFileName);

            // 手动设置高级下载接口的并发数 (默认为5), 从5.4.26版本开始支持！
            //downloadTask.SetMaxTasks(10);
            downloadTask.progressCallback = delegate (long completed, long total)
            {
                Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
            };

            try
            {
                COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await transferManager.DownloadAsync(downloadTask);
                string eTag = result.eTag;
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }

        }
        public static void Main(string filename,string filepath)
        {

            TransferDownloadObjectModel m = new TransferDownloadObjectModel();

            /// 高级接口下载对象
            m.TransferDownloadObject(filename,filepath).Wait();

        }
    }
}
    
    public class GetObjectModel
    {

        private CosXml cosXml;

        public GetObjectModel()
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
              .SetRegion("ap-shanghai") // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 
              .Build();

            string secretId = "AKIDNf2PoDQfjgaHtJZI2mLhoi1eEeuAF9sP";   // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            string secretKey = "Yv8A1iFbnphNJ4eQOEzkGex6CC5qOhkW"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
            long durationSecond = 600;          //每次请求签名有效时长，单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
              secretKey, durationSecond);

            this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }

        /// 下载对象
        public void GetObject(string filename,string filepath)
        {
            //.cssg-snippet-body-start:[get-object]
            try
            {
                // 存储桶名称，此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
                string bucket = "document-manage-1317383760";
                string key = "实习文件/"; //对象键
                string localDir = filepath;//本地文件夹
                string localFileName = filename; //指定本地保存的文件名
                GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
                //设置进度回调
                request.SetCosProgressCallback(delegate (long completed, long total)
                {
                    Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
                });
                //执行请求
                GetObjectResult result = cosXml.GetObject(request);
                //请求成功
                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
        }

        //static void Main(string[] args)
        //{
        //    GetObjectModel m = new GetObjectModel();
        //    /// 下载对象
        //    m.GetObject();
        //}
    }

    
        #endregion
